/**
 * 获取格式化后的路由
 *
 * @param {Array} menus 菜单数据
 * @param {string} parentId 父级ID
 * @returns {Array} 格式化后的路由
 */
export function getFormatRoutes(menus, parentId = '0', level = 1) {
  const routes = menus
    .filter(menu => {
      return menu.parentId === parentId;
    })
    .sort((pre, next) => (pre.orderId >= next.orderId ? 1 : -1))
    .map(m => {
      const children = getFormatRoutes(menus, m.id, level + 1);
      const path = m.vueRouter ? m.vueRouter : `/compage/${m.id}`;
      const hideInMenu = !(m.type == 1 || (m.type == 4 && children.filter(i => !i.hideInMenu).length));
      const route = {
        path,
        component: m.parentId != '0' ? 'layout.blank' : 'layout.base',
        name: m.parentId != '0' ? `compage${m.parentId}_` + `compage${m.id}` : `compage${m.id}`,
        hideInMenu,
        key: m.id,
        outside: true,
        meta: {
          title: m.name,
          icon: m.iconCode || 'ep:menu',
          // localIcon: 'custom-icon',
          // activeMenu: '',
          keepAlive: m.type == '1',
          code: m.id,
          hideInMenu,
          level
        }
      };
      if (children && children.length) {
        route.children = children;
      }
      if (children && children.filter(i => !i.hideInMenu).length) {
      } else if (m.vueComponent) {
        route.component = m.vueComponent;
      } else if (m.type == 1) {
        route.component = 'keepcommonpage';
      } else {
        route.component = 'commonpage';
      }
      return route;
    });
  return routes;
}
